import { app } from 'electron';
import * as fs from 'fs';
import * as path from 'path';

// 确保日志目录存在
// const logDir = path.join(__dirname, 'logs');
const logDir = path.join(app.getPath('userData'), 'logs');
if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}
console.log(`日志目录: ${logDir}`);

const logFile = path.join(logDir, 'server.log');
const logStream = fs.createWriteStream(logFile, { flags: 'a', encoding: 'utf8' });

function logMessage(level: string, message: string, ...args: any[]) {
    const formattedMessage = `[${level}] ${new Date().toISOString()} ${message} ${args.join(' ')}\n`;
    console.log(formattedMessage);
    logStream.write(formattedMessage);
}

export const logger = {
    info: (message: string, ...args: any[]) => {
        logMessage('INFO', message, ...args);
    },
    error: (message: string, ...args: any[]) => {
        logMessage('ERROR', message, ...args);
    },
};
